package itsm.isperp.framework.data.mybatis.domain;

import itsm.isperp.framework.collection.tree.ITreeNode;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;

/**
 * 一个基本的实现树形节点模型 {@link ITreeNode} 的节点实体
 * 
 * @author lizx
 * @date 2013-2-5
 * @version V1.0
 */
@MappedSuperclass
public class AbstractTreeNode implements ITreeNode {

	/**
	 * 
	 */
	protected static final long serialVersionUID = -3531004225346792824L;

	/**
	 * 标识
	 */
	@Id
	@Column
	protected String id;
	/**
	 * 名字
	 */
	@Column
	protected String name;
	/**
	 * 上级节点
	 */
	@Column
	protected String parentId;

	/**
	 * 路径
	 */
	@Column
	protected String treenodePath;

	/**
	 * 名称路径
	 */
	@Column
	protected String treenodeNamePath;

	@Column
	protected boolean open;

	@Column
	protected boolean leaf;

	@Column
	protected Integer orderNum;

	/**
	 * 是否打勾
	 */
	protected Boolean checked;

	@Column
	protected String icon;

	/**
	 * 直接子节点集合
	 */
	protected List<? extends ITreeNode> children;

	public AbstractTreeNode() {

	}

	public AbstractTreeNode(ITreeNode t) throws IllegalAccessException,
			InvocationTargetException {
		BeanUtils.copyProperties(this, t);
	}

	/**
	 * 默认根据是否有子节点判断
	 */
	public boolean isIsParent() {
		return !leaf;
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getParentId() {
		return parentId;
	}

	@Override
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	@Override
	public String getText() {
		return this.name;
	}

	@Override
	public String getTreenodePath() {
		return this.treenodePath;
	}

	@Override
	public void setTreenodePath(String treenodePath) {
		this.treenodePath = treenodePath;
	}

	@Override
	public String getTreenodeNamePath() {
		return this.treenodeNamePath;
	}

	@Override
	public void setTreenodeNamePath(String treenodeNamePath) {
	 
		this.treenodeNamePath = treenodeNamePath;
	}

	public boolean isOpen() {
		return open;
	}

	public void setOpen(boolean open) {
		this.open = open;
	}

	public Integer getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(Integer orderNum) {
		this.orderNum = orderNum;
	}

	@Override
	public List<? extends ITreeNode> getChildren() {
		return children;
	}

	@Override
	public void setChildren(List<? extends ITreeNode> children) {
		this.children = children;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof AbstractTreeNode)) {
			return false;
		}
		AbstractTreeNode o = (AbstractTreeNode) object;
		// 类型和ID要一致
		return new EqualsBuilder()
				.append(this.getClass().getName(), o.getClass().getName())
				.append(this.getId(), o.getId()).isEquals();
	}

}